package banmuoi.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import banmuoi.entity.Category;
import banmuoi.entity.Customer;
import banmuoi.entity.Order;
import banmuoi.entity.OrderDetail;
import banmuoi.entity.Product;
import banmuoi.util.*;

@WebServlet("/crudOrderDetailCustomer.do")
public class OrderDetailCustom extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String orderId = req.getParameter("orderid");
		
		Session session = HibernateUtil.openSession();
	
		//HIỆN DANH SÁCH
		if(orderId != null && orderId != ""){
			int idOrder = Integer.parseInt(orderId);
			Order entity = (Order) session.load(Order.class, idOrder);
			req.setAttribute("entity", entity);
			
			float total = 0;
			
			for(OrderDetail item : entity.getOrderDetails() ){
				   total += item.getAmount();
			}
			
			UpdateTotalOnOrder(idOrder, total);
			req.setAttribute("total", total);
			
			//Show product
			String hql = "FROM Product";
			Query query = session.createQuery(hql);
			List<Product> entities = query.list();
			req.setAttribute("showProduct", entities);
			
			if (req.getParameter("detailid") != null && req.getParameter("detailid") != "")
			{
				int detailId = Integer.parseInt(req.getParameter("detailid"));
				OrderDetail details = (OrderDetail) session.load(OrderDetail.class, detailId);
				req.setAttribute("entityOrderDetail", details);
			}
	
			
		}else
		{
			req.getRequestDispatcher("OrderCustomer.jsp").forward(req, resp);
			session.close();
			return;
		}
		
		req.getRequestDispatcher("OrderDetailCustomer.jsp").forward(req, resp);			
		session.close();
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.openSession();
		Transaction trans = session.beginTransaction();
		
		try
		{
			if (req.getParameter("btnCreate") != null)
			{
				//Thêm
				int orderId = Integer.parseInt(req.getParameter("orderid"));
				int productId = Integer.parseInt(req.getParameter("cboProducts"));
				int quantity = Integer.parseInt(req.getParameter("txtQuantity"));
				OrderDetail or = new OrderDetail();
				Order o = new Order();
				Product details = (Product) session.load(Product.class, productId);
				o.setId(orderId);
				
				or.setOrder(o);
				or.setAmount((float)(details.getUnitPrice() * quantity));
				or.setQuantity(quantity);
				or.setProduct(details);
				
				session.save(or);
				req.setAttribute("errorDetail", "Insert Success!");
			} else if (req.getParameter("btnUpdate") != null)
			{
				int orderId = Integer.parseInt(req.getParameter("orderid"));
				int detailId = Integer.parseInt(req.getParameter("txtIdOrderDetail"));
				int productId = Integer.parseInt(req.getParameter("cboProducts"));
				int quantity = Integer.parseInt(req.getParameter("txtQuantity"));
				OrderDetail detail = (OrderDetail) session.load(OrderDetail.class, detailId);
				Product details = (Product) session.load(Product.class, productId);
				detail.setAmount((float)(details.getUnitPrice() * quantity));
				detail.setQuantity(quantity);
				detail.setProduct(details);
				
				session.update(detail);
				req.setAttribute("errorDetail", "Update Success!");
			}
			else if (req.getParameter("btnDelete") != null)
			{
				
				int detailId = Integer.parseInt(req.getParameter("txtIdOrderDetail"));
				OrderDetail detail = new OrderDetail();
				
				detail.setId(detailId);
				
				session.delete(detail);
				req.setAttribute("errorDetail", "Delete Success!");
			}
			
			trans.commit();
		} catch (Exception ex)
		{
			trans.rollback();
			req.setAttribute("errorDetail", ex.getMessage());
			
		}
		
		
		
		doGet(req, resp);
	}
	
	private void UpdateTotalOnOrder(int orderId, float total)
	{
		Session session = HibernateUtil.openSession();
		Transaction trans = session.beginTransaction();
		
		Order entity = (Order) session.load(Order.class, orderId);
		entity.setTotal(total);
		
		session.update(entity);
		trans.commit();
		session.close();
	}
}
